$("#mfile").fileinput({
    showPreview: true,
    showUpload: true,
    elErrorContainer: '#kartik-file-errors',
    uploadUrl: '/upload/file?type=banner',
    allowedFileExtensions: ["jpg", "jpeg", "png", "gif"]
}).on("fileuploaded", function (event, data) {
    console.log(data);
    $("#hidpic").val(data.response.data);
})

function changeSate(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/faxing/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    layer.confirm('您确定要删除此行数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    layer.msg('删除成功', {icon: 1});
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/faxing/list'});
                } else {
                    layer.msg('删除失败', {icon: 1});
                }
            },
            dataType: "json"
        });

    }, function () {
        layer.msg('您已取消操作', {
            time: 1000, //20s后自动关闭
            btn: ['明白了', '知道了']
        });
    });
}

var editsw = [];
/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var id = button.data('row');
    console.log(button.data('row'))
    var modal = $(this);
    $.ajax({
        type: 'POST',
        url: "getFaxingById",
        data: {
            id: id
        },
        success: function (data) {
            console.log(data)

            modal.find('#id').val(data.id)
            modal.find('#name').val(data.name)
            modal.find('#code').val(data.code)

            modal.find('#thumb').val(data.thumb)
            if (data.ismain == 1) {
                modal.find('#ismain').attr("checked", true);
            }
            editsw["ismain"] = new Switchery(document.querySelector('#ismain'), {color: "#1AB394"})
            modal.find('#mpid').val(data.mpid)
            modal.find('#sid').val(data.sid)

            if (data.state == 1) {
                modal.find('#state').attr("checked", 'checked').val("1");
            }
        },
        dataType: "json"
    });
})

//tableHeight函数
function tableHeight() {
    //可以根据自己页面情况进行调整
    return $(window).height() - 200;
}

$(function () {
    var sw = [];
    $(".js-switch").each(function (i, e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })

    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })

    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url: "/admin/faxing/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination: true,//是否分页
        queryParamsType: 'limit',
        queryParams: queryParams,
        sidePagination: 'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize: 50,//单页记录数
        pageList: [50, 100, 200, 500],//分页步进值
        showExport: true,//显示导出
        showRefresh: false,//刷新按钮
        showColumns: false,
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns: [
            {
                title: '全选',
                field: 'select',
                checkbox: true,
                width: 25,
                align: 'center',
                valign: 'middle'
            },
            {
                title: 'ID',
                field: 'id',
                visible: false
            },
            {
                title: '名称',
                field: 'name'
            },
            {
                title: '类型',
                field: 'code'
            },
            {
                title: '图路径',
                field: 'pic',
                formatter: "imgFormatter"
            },
            {
                title: '资源缩略图',
                field: 'thumb'
            },
            {
                title: '主图片',
                field: 'ismain'
            },
            {
                title: '操作',
                field: 'Attribute',
                align: 'center',
                formatter: actionFormatter
            }
        ],
        locale: 'zh-CN',//中文支持,
    })

    //请求服务数据时所传参数
    function queryParams(params) {
        console.log(params)
        return params;
    }

})